package com.example.exceltosqlite.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.exceltosqlite.entity.SqliteFileEntity;
import com.example.exceltosqlite.entity.SqliteFileNumberEntity;
import com.example.exceltosqlite.entity.SqliteFileTypeEntity;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;


/**
 * version 1  为基础数据库
 */
public class SQLliteOpenHelper extends OrmLiteSqliteOpenHelper {


    private static final String DB_NAME = "BKTelegramDB.db";
    private static final int DATABASE_VERSION = 1;


    public SQLliteOpenHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, SqliteFileTypeEntity.class);
            TableUtils.createTable(connectionSource, SqliteFileNumberEntity.class);
            TableUtils.createTable(connectionSource, SqliteFileEntity.class);
            sqLiteDatabase.execSQL("INSERT INTO telegramType (type) VALUES ('类型01')");
            sqLiteDatabase.execSQL("INSERT INTO telegramType (type) VALUES ('类型02')");
            sqLiteDatabase.execSQL("INSERT INTO telegramType (type) VALUES ('类型03')");
            sqLiteDatabase.execSQL("INSERT INTO telegramType (type) VALUES ('类型04')");

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
//        switch (oldVersion) {
//            case 1:
//            sqLiteDatabase.execSQL("INSERT INTO telegramType (type) VALUES ('类型05')");
//            sqLiteDatabase.execSQL("INSERT INTO telegramType (type) VALUES ('类型06')");
//            break;
//        }

    }


    //实现一个单例返回DbHelper实例
    private static SQLliteOpenHelper helper;

    public static SQLliteOpenHelper getHelper(Context context) {
        if (helper == null) {
            helper = new SQLliteOpenHelper(context);
        }
        return helper;
    }

    public static SQLliteOpenHelper getHelper() {
        return helper;
    }

    public static void setHelper(SQLliteOpenHelper helper) {
        SQLliteOpenHelper.helper = helper;
    }


}
